package cn.tedu.zx.ams.adapter.serviceimpl;

import cn.tedu.zx.ams.adapter.mapper.LoginLogMapper;
import cn.tedu.zx.ams.service.LoginLogService;
import cn.tedu.zx.common.exception.ServiceException;
import cn.tedu.zx.pojo.admin.entity.LoginLog;
import cn.tedu.zx.pojo.admin.parm.LoginLogAddNewParam;
import cn.tedu.zx.pojo.admin.vo.LoginLogListItemVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

import static cn.tedu.zx.common.restful.ResponseCode.ERR_INSERT;

@Service
@Slf4j
public class LoginLogServiceImpl implements LoginLogService {

    @Autowired
    private LoginLogMapper loginLogMapper;

    @Override
    public void addNew(LoginLogAddNewParam loginLogAddNewParam) {
        log.debug("开始执行【添加管理日志】的业务");
        LoginLog loginLog = new LoginLog();
        BeanUtils.copyProperties(loginLogAddNewParam, loginLog);
        loginLog.setCreated(LocalDateTime.now());
        int insertCount = loginLogMapper.insert(loginLog);
        if (insertCount != 1) {
            String message = "添加管理日志失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ERR_INSERT, message);
        }
        log.debug("已完成【添加管理日志】的业务");
    }

    @Override
    public List<LoginLogListItemVO> list() {
        return loginLogMapper.list();
    }
}
